package cn.xfdzx.android.apps.shop.adapter;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.bean.DetailPringBean;
import cn.xfdzx.android.apps.shop.bean.OneCategoryBeen;
import cn.xfdzx.android.apps.shop.bean.ShopAddGoodTypeBeen;

/**
 * Creator: zf.
 * Date: 2020/8/20.
 * Description:商家店铺分类列表适配器.
 */
public class ShopCatoryListAdapter extends BaseQuickAdapter<ShopAddGoodTypeBeen.DataBean, BaseViewHolder> {
    private List<ShopAddGoodTypeBeen.DataBean> dataBeans = new ArrayList<>();//分类列表
    private ShopCatoryListAdapter.OnItemItemCatoryClickListener onItemOnClickListener;
    private int oldPosition;
    public ShopCatoryListAdapter(@Nullable List<ShopAddGoodTypeBeen.DataBean> data) {
        super(R.layout.item_shopcatorylist, data);
        this.dataBeans = data;
    }

    @Override
    public void setData(int index, @NonNull ShopAddGoodTypeBeen.DataBean data) {
        super.setData(index, data);
    }


    @Override
    protected void convert(final BaseViewHolder viewHolder, final ShopAddGoodTypeBeen.DataBean date) {

        ImageView imageView = viewHolder.getView(R.id.img_sel);
        TextView tv_title = viewHolder.getView(R.id.tv_title);
        viewHolder.setText(R.id.tv_title,date.getName());

        if (date.isCheck()){
            imageView.setVisibility(View.VISIBLE);
            tv_title.setTextColor(mContext.getResources().getColor(R.color.color_FB9129));
        }else {
            imageView.setVisibility(View.GONE);
            tv_title.setTextColor(mContext.getResources().getColor(R.color.color_333333));
        }

        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (oldPosition ==viewHolder.getLayoutPosition()){
                    return;
                }else {
                    dataBeans.get(oldPosition).setCheck(false);
                    dataBeans.get(viewHolder.getLayoutPosition()).setCheck(true);
                    onItemOnClickListener.onItemItemCatoryClick(viewHolder.getLayoutPosition(),dataBeans);
                    oldPosition = viewHolder.getLayoutPosition();
                }

            }
        });

    }
    public void clearDatas(){
        mData.clear();
        notifyDataSetChanged();
    }
    public void refreshDatas(List<ShopAddGoodTypeBeen.DataBean> list){
        this.dataBeans = list;
        super.replaceData(list);
    }

    public void addDatas(List<ShopAddGoodTypeBeen.DataBean>  list){
        super.addData(list);
        //mData.addAll(list);
    }

    public void setItemCatoryClickListener(ShopCatoryListAdapter.OnItemItemCatoryClickListener listener){
        this.onItemOnClickListener = listener;
    }

    public interface OnItemItemCatoryClickListener {
        void onItemItemCatoryClick( int position,List<ShopAddGoodTypeBeen.DataBean> catoryDate);
    }

}
